package com.java110.mall.cart.bmo.gold.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.mall.cart.bmo.gold.IComputeGiveGoldBMO;
import com.java110.dto.coupon.CouponConfDto;
import com.java110.dto.vipUser.ShopVipLevelDto;
import com.java110.dto.vipUser.ShopVipUserDto;
import com.java110.intf.shop.ICouponConfInnerServiceSMO;
import com.java110.intf.shop.IShopVipLevelInnerServiceSMO;
import com.java110.intf.shop.IShopVipUserInnerServiceSMO;
import com.java110.utils.util.Assert;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service("computeGiveGoldBMO")
public class ComputeGiveGoldBMO implements IComputeGiveGoldBMO {

    @Autowired
    private IShopVipUserInnerServiceSMO shopVipUserInnerServiceSMOImpl;

    @Autowired
    private IShopVipLevelInnerServiceSMO shopVipLevelInnerServiceSMOImpl;
    @Autowired
    private ICouponConfInnerServiceSMO couponConfInnerServiceSMOImpl;

    /**
     * @param param
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> compute(JSONObject param) {

        String shopId = param.getString("shopId");
        String vipId = param.getString("vipId");
        double amount = param.getDouble("amount");
        if (amount == 0) {
            JSONObject paramIn = new JSONObject();
            paramIn.put("goldCount", 0);
            paramIn.put("integralCount", 0);
            return ResultVo.createResponseEntity(paramIn);
        }

        //查询会员是否存在
        ShopVipUserDto shopVipUserDto = new ShopVipUserDto();
        shopVipUserDto.setShopId(shopId);
        shopVipUserDto.setVipId(vipId);
        List<ShopVipUserDto> shopVipUserDtos = shopVipUserInnerServiceSMOImpl.queryShopVipUsers(shopVipUserDto);

        Assert.listOnlyOne(shopVipUserDtos, "会员不存在");

        ShopVipLevelDto shopVipLevelDto = new ShopVipLevelDto();
        shopVipLevelDto.setShopId(shopId);
        shopVipLevelDto.setVipLevelId(shopVipUserDtos.get(0).getVipLevel());
        List<ShopVipLevelDto> shopVipLevelDtos = shopVipLevelInnerServiceSMOImpl.queryShopVipLevels(shopVipLevelDto);
        Assert.listOnlyOne(shopVipLevelDtos, "会员等级不存在");
        // "goldCostMoney":"200.00","integralCostMoney":"100.00"
        CouponConfDto couponConfDto = new CouponConfDto();
        couponConfDto.setShopId(shopVipLevelDtos.get(0).getShopId());
        couponConfDto.setVipLevelId(shopVipLevelDtos.get(0).getVipLevelId());
        List<CouponConfDto> couponConfDtos = couponConfInnerServiceSMOImpl.queryCouponConfs(couponConfDto);


        double goldCostMoney = Double.parseDouble(shopVipLevelDtos.get(0).getGoldCostMoney());

        double goldCount = Math.floor(amount / goldCostMoney);
        BigDecimal newGoldCount = new BigDecimal(0);
        if (couponConfDtos != null && couponConfDtos.size() > 0) {
            for (CouponConfDto couponConf : couponConfDtos) {
                BigDecimal tempcouponCount = new BigDecimal(couponConf.getCouponCount());
                BigDecimal price = tempcouponCount.multiply(new BigDecimal(goldCount)).setScale( 0, BigDecimal.ROUND_DOWN );
                newGoldCount = newGoldCount.add(price).setScale( 0, BigDecimal.ROUND_DOWN );
            }
        }
        goldCount = newGoldCount.doubleValue();
        double integralCount = Math.floor(amount / Double.parseDouble(shopVipLevelDtos.get(0).getIntegralCostMoney()));

        JSONObject paramIn = new JSONObject();
        paramIn.put("goldCount", goldCount);
        paramIn.put("integralCount", integralCount);

        return ResultVo.createResponseEntity(paramIn);
    }

}